//
//  httpdownloadengine.h
//  Etion
//
//  Created by x w on 11-2-15.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "XLGDownloadEnum.h"

#import "XLGDownloadManagerInternalDelegate.h"

#import "XLGDownloadPoolItem+XLGDownloadPoolInternal.h"

#import "RequestChannel.h"

#define FILEDATABUFFER		204800

@interface XLGDownloadOperationCore : CRequestChannel
{
    XMSDispatchQueueWrapper *_delegateQueue;
}

@property(nonatomic,readonly) XMSDispatchQueueWrapper *delegateQueue;

@end

@interface XLGDownloadPoolItem (XLGDownloadOperation)

- (XLGDownloadOperation*)downloadOperationWithDownloadIndex:(XLGDownloadIndex*)downloadIndex;

@end

@protocol XLGDownloadOperationDelegate <NSObject>

@optional

-(void)downloadOperationItem:(NSString*)itemExtendID refreshPercentWithFileTotalSize:(NSUInteger)totalSize downloadedSize:(NSUInteger)downloadedSize;

@end

@interface XLGDownloadOperation : XLGDownloadPoolItem
{
	NSMutableData *_fileData;

	NSFileHandle* _downloadFileHandle;
	
	//文件当前大小
	NSUInteger _fileCurrentSize;
	NSUInteger _fileTotalSize;
	
	NSString* _filePath;
	XLGDownloadIndex* _mi;
	BOOL _bFinish;

    id<XLGDownloadManagerInternalDelegate> _internalDelegate;
    
    NSMutableArray* _arUIdelegate;
    
@private
    XLGDownloadOperationCore *_core;
}

@property(nonatomic,readonly) NSString* filePath;
@property(nonatomic,assign) id<XLGDownloadManagerInternalDelegate> internalDelegate;
@property(nonatomic,readonly) XLGDownloadIndex* mi;
@property(nonatomic,readonly) NSMutableArray* arUIdelegate;

-(void)addOperationDelegate:(id<XLGDownloadOperationDelegate>)delegate;

-(void)addOperationDelegateFromArray:(NSArray*)arDelegate;

-(void)removeOperationDelegate:(id<XLGDownloadOperationDelegate>)delegate;

-(void)removeAllOperationdelegate;

-(id)initWithDownloadIndex:(XLGDownloadIndex*)mi;

//-(void)refreshDownloadOperationPercent;

-(void)stopDownload;

-(void)writeDataToFile:(NSData*)data;

-(void)initFileData;

-(void)cleanup;

-(void)downloadError;

-(void)downloadFinished;

@end
